package com.aigestudio.demo.com.aigestudio.tabview;

import android.view.View;

import java.util.Calendar;

/**
 * Created by Fcy on 2016/6/18.
 */
public class TimeController implements ITimeTabData {

    private long mFromTime, mEndTime; // s
    private int[] mTimeKeyArr;
    private View btnConfirm;
    private ArrowEditView leftEditView, rightEditView;

    public TimeController(int[] keyArr) {
        bindTimeKeyArr(keyArr);
    }

    public long getFromTime() {
        return mFromTime;
    }

    public long getEndTime() {
        return mEndTime;
    }

    public long getDate(boolean isFromTime){
        return 1000 * (isFromTime ? mFromTime : mEndTime);
    }

    public int[] getDates(boolean isFromTime) {
        // 2016/6/24
        int[] result = new int[3];
        Calendar cal = Calendar.getInstance();
        if (isFromTime) {
            cal.setTimeInMillis(mFromTime * 1000);
        } else {
            cal.setTimeInMillis(mEndTime * 1000);
        }

        result[0] = cal.get(Calendar.YEAR);
        result[1] = getRealMonth(cal.get(Calendar.MONTH));
        result[2] = cal.get(Calendar.DAY_OF_WEEK);
        return result;
    }

    public int getRealMonth(int month){
        if (month == 12){
            return month;
        } else {
            return month + 1;
        }
    }

	public static TimeController getInstance(int[] keyArr) {
		return new TimeController(keyArr);
	}

    private void bindTimeKeyArr(int[] keyArr) {
        mTimeKeyArr = keyArr;
    }

    /**
     * @param leftEditView
     * @param rightEditView
     * @param btnConfirm
     *         确定按钮
     */
    public void bindEditView(ArrowEditView leftEditView, ArrowEditView rightEditView,
                             View btnConfirm) {
        this.leftEditView = leftEditView;
        this.rightEditView = rightEditView;
        this.btnConfirm = btnConfirm;

        initClick(leftEditView, 0);
        initClick(rightEditView, 1);
		btnConfirm.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// 点击确定按钮，发起请求
				if (mCustomTimeAction != null){
					mCustomTimeAction.ConfirmCustomTime();
				}
			}
		});
    }

    public boolean checkTime(){
        return mEndTime >= mFromTime;
    }

    private void initClick(final ArrowEditView editView, final int position) {
        editView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (editView.isEditState() && mCustomTimeAction != null) {
                    // 在实现该接口的地方，根据用户自定义的时间调用 setCustomTime
                    mCustomTimeAction.clickCustomTime(position);
                }
            }
        });
    }

    public int[] getTimeKeyArr() {
        return mTimeKeyArr;
    }

    /**
     * 设置自定义时间，根据时间选择器
     */
    public void setCustomTime(long fromTime,long endTime) {
        setFromTime(fromTime);
		setEndTime(endTime);
    }

	/**
	 * @param time ms
	 */
	public void setFromTime(long time){
		mFromTime = time / 1000;
	}

	/**
	 * @param time ms
	 */
	public void setEndTime(long time){
		mEndTime = time / 1000;
	}

    public void resetTimeUI() {
        leftEditView.getTextView().setText(DateUtil.getTimeStr(mFromTime));
        rightEditView.getTextView().setText(DateUtil.getTimeStr(mEndTime));
    }

    /**
     * @param position
     *         选中的位置
     */
    public void resetTime(int position) {
        if (mTimeKeyArr != null && mTimeKeyArr.length > position) {
            changeState(mTimeKeyArr[position] == CUSTOM);
            if (mTimeKeyArr[position] == CUSTOM) {
                // 如果是自定义
                if (mCustomTimeAction != null) {
                    // 在实现该接口的地方，根据用户自定义的时间调用 setCustomTime
                    mCustomTimeAction.clickCustomTime(0);
                }
            } else {
                // 计算出每个时间段的时间戳，直接赋值给mFromTime, mEndTime
                switch (mTimeKeyArr[position]) {
                    case TODAY:
                        mEndTime = mFromTime = DateUtil.getCurrDayTime();
                        break;
                    case WEEK:
                        mFromTime = DateUtil.getCurrMondayTime();
                        mEndTime = DateUtil.getCurrSundayTime();
                        break;
                    case MOUTH:
                        mFromTime = DateUtil.getMinMonthTime();
                        mEndTime = DateUtil.getMaxMonthTime();
                        break;
                }

                resetTimeUI();
            }

        }
    }

    /**
     * 切换状态
     *
     * @param isEdit
     *         是否是编辑
     */
    public void changeState(boolean isEdit) {
        leftEditView.changeState(isEdit);
        rightEditView.changeState(isEdit);
        btnConfirm.setVisibility(isEdit ? View.VISIBLE : View.GONE);
    }

    private ICustomTimeAction mCustomTimeAction;

    public void setCustomTimeAction(ICustomTimeAction mCustomTimeAction) {
        this.mCustomTimeAction = mCustomTimeAction;
    }

    public interface ICustomTimeAction {

        void clickCustomTime(int position);

		void ConfirmCustomTime();
    }
}
